package com.hygj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hygj.pojo.RandomOffice;

import com.hygj.service.RandomOfficeService;
import com.hygj.util.PageUtil;

@Controller
@RequestMapping
public class RandomOfficeController {
	
	@Autowired
	private RandomOfficeService randomOfficeService;
	
	
	/**
	 * 保存
	 * @param map
	 * @param randomOffice
	 * @return
	 */
	@RequestMapping("insertOffice")
	public String insertOffice(Map<String, Object> map, RandomOffice randomOffice) {
		randomOfficeService.insert("insert", randomOffice);
		map.put("randomOffice", randomOffice);
		return "redirect:getOfficeAll";
	}
	
	/**
	 * 查询全部
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("getOfficeAll")
	public String getOfficeAll(Map<String, Object> map,HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();		
		PageUtil<RandomOffice> page = randomOfficeService.page(request, 10, params);
		map.put("page", page);
		return "random/randomOfficeShow";
	}
	
	/**
	 * 修改(服务单位名称)
	 * @param randomOffice
	 * @return
	 */
	@RequestMapping("updateOffice")
	public String updateOffice(RandomOffice randomOffice){
		randomOfficeService.update("update", randomOffice);
		return "redirect:getOfficeAll";
	}
	
	/**
	 * 修改(禁用|启用)
	 * @param randomOffice
	 * @return
	 */
	@RequestMapping("updateOffices")
	public String updateOffices(RandomOffice randomOffice){
		randomOfficeService.update("update", randomOffice);
		return "redirect:getOfficeAll";
	}
	
	/**
	 * 详情页面
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getOfficeById")
	public String getOfficeById(@RequestParam("id") Integer id, ModelMap modelMap) {
		RandomOffice randomOffices = randomOfficeService.getById("getById", id);
		modelMap.addAttribute("randomOffices", randomOffices);
		return "random/randomOfficeUpdate";
	}
	
	/**
	 * 详情页面
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getOfficeByIds")
	public String getOfficeByIds(@RequestParam("id") Integer id, ModelMap modelMap) {
		RandomOffice randomOffices = randomOfficeService.getById("getById", id);
		modelMap.addAttribute("randomOffices", randomOffices);
		return "random/randomOfficeOpen";
	}
	
	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteOffice")
	public String deleteOffice(@RequestParam("id") Integer id) {
		randomOfficeService.delete("delete", id);
		return "redirect:getOfficeAll";
	}
	
	/**
	 * 随机抽取
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("look")
	public List<RandomOffice> randomNum(HttpServletResponse response, HttpServletRequest request) {
		List<RandomOffice> randomOffice = randomOfficeService.findRandomByName("启用");
		return randomOffice;
	}

}
